from pypinyin import pinyin, Style
import re


# 简单使用一下


# 将中文数字转换为阿拉伯数字
def chinese_to_arabic_func(match):
    chinese_to_arabic = {'零': '0', '一': '1', '二': '2', '三': '3', '四': '4', '五': '5', '六': '6', '七': '7', '八': '8',
                         '九': '9',
                         '十': '1'}
    chinese_num = match.group()
    arabic_num = ''
    if chinese_num == "十":
        arabic_num = " 10 "
        return arabic_num
    for char in chinese_num:
        if char in chinese_to_arabic:
            arabic_num += chinese_to_arabic[char]
    return " " + arabic_num + " "


def chinese_to_set_func(str):
    # 切开最后三个字符前面的  ”田螺塘“ 十六巷
    original_content = str[:-3]
    # 切开最后三个字符前面的  田螺塘 “十六巷”
    last_three_chars = str[-3:]
    # 把中文换成阿拉伯数字 十六巷 16巷
    replaced_last_three_chars = re.sub(r'[零一二三四五六七八九十]+', chinese_to_arabic_func, last_three_chars)
    # 拼接到原来的字符串里面
    original_content = original_content + replaced_last_three_chars
    result = ''.join([item[0] for item in pinyin(original_content, style=Style.TONE)])
    # result = ''.join(add_apostrophe_to_first_vowel(original_content))
    #  首字母大写
    result_capitalized = result.capitalize()


    return result_capitalized


def add_apostrophe_to_first_vowel(original_content):
    # 获取原始文本的拼音列表
    pinyin_list = pinyin(original_content, style=Style.TONE3)

    # 定义元音字母的集合
    vowels = {'a', 'e', 'i', 'o', 'u', 'ü'}

    # 对第一个拼音进行处理
    first_py = ''.join(pinyin_list[0])
    first_vowel_index = next((i for i, c in enumerate(first_py) if c in vowels), None)

    # 如果存在元音字母，并且是首字母，则在其前插入 "'"
    if first_vowel_index == 0:
        first_py = "'" + first_py

    # 将处理后的首个拼音加入结果中
    result = first_py

    # 处理剩余的拼音并将其加入结果中
    for py in pinyin_list[1:]:
        py_str = ''.join(py)
        result += py_str + ' '

    return result.strip()


